package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.AttributeTemplateAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateUpdateInfoParam;
import cn.tedu.csmall.product.service.AttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@RequestMapping("/attribute-template/")
@Api(tags = "06.属性模板管理模块")
public class AttributeTemplateController {

    @Autowired
    private AttributeTemplateService attributeTemplateService;

    @PostMapping("add-new")
    @ApiOperation("添加属性模板")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(AttributeTemplateAddNewParam attributeTemplateAddNewParam, @ApiIgnore HttpSession session) {
        attributeTemplateService.addNew(attributeTemplateAddNewParam);
        return JsonResult.success();
    }

    @PostMapping("delete")
    @ApiOperation("根据id删除属性模板")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "attributeTemplateId", value = "属性模板ID", required = true, dataType = "long")// 对未封装的请求参数进行描述
    public JsonResult delete(@Range(min = 1, message = "属性模板ID不合法！")
                             @RequestParam Long attributeTemplateId) {
        attributeTemplateService.deleteById(attributeTemplateId);
        return JsonResult.success();
    }

    @PostMapping("updateInfoById")
    @ApiOperation("更改属性模板详情信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的属性模板id") Long id,
                                     @Valid AttributeTemplateUpdateInfoParam attributeTemplateUpdateInfoParam) {
        attributeTemplateService.updateInfoById(id,attributeTemplateUpdateInfoParam);
        return JsonResult.success();
    }

    @GetMapping("list")
    @ApiOperation("分页查询属性模板列表")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int")
    public JsonResult list(@RequestParam @Range(min = 1, message = "查询属性模板列表失败，请提供正确的页码值！") Integer pageNum) {
        return JsonResult.success(attributeTemplateService.list(pageNum));
    }

    @GetMapping("listAll")
    @ApiOperation("查询所有属性模板列表")
    @ApiOperationSupport(order = 401)
    public JsonResult listAll() {
        Integer pageNum = 1;
        Integer pageSize = Integer.MAX_VALUE;
        return JsonResult.success(attributeTemplateService.list(pageNum, pageSize));
    }

    @GetMapping("getStandardById")
    @ApiOperation("根据id查询一条数据")
    @ApiOperationSupport(order = 410)
    public JsonResult getStandardById(@RequestParam @Range(min = 1, message = "根据ID查询属性模板失败，请提供正确的ID值！") Long id) {
        return JsonResult.success(attributeTemplateService.getStandardById(id));
    }
}
